package com.theme.passport.controller.theme;

import com.theme.common.mvc.utils.PageUtils;
import com.theme.common.mvc.view.R;
import com.theme.passport.entity.theme.WidgeEntity;
import com.theme.passport.entity.theme.WidgeLayoutEntity;
import com.theme.passport.service.theme.WidgeLayoutService;
import com.theme.passport.service.theme.WidgeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;



/**
 * 组件配置表
 *
 * @author live
 * @email 870459550@qq.com
 * @date 2022-06-21 10:01:48
 */
@RestController
@RequestMapping("widge/widge-layout")
public class WidgeLayoutController{
    @Autowired
    private WidgeLayoutService widgeLayoutService;

    @Autowired
    private WidgeService widgeService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("widge:widge-layout:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = widgeLayoutService.queryPage(params);
        List<WidgeLayoutEntity> list = (List<WidgeLayoutEntity>) page.getList();
        list.forEach(widgeLayout ->{
            WidgeEntity wdgeEntity = widgeService.getById(widgeLayout.getWidgeId());
            widgeLayout.setWidgeName(wdgeEntity.getName());
        });
        page.setList(list);
        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/widgeList")
    //@RequiresPermissions("widge:widge-layout:list")
    public R widgeList(@RequestParam Map<String, Object> params){
        List<WidgeEntity> list = widgeLayoutService.queryWidgeList(params);
        return R.ok().put("list", list);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("widge:widge-layout:info")
    public R info(@PathVariable("id") Long id){
		WidgeLayoutEntity wqidgeLayout = widgeLayoutService.getById(id);

        return R.ok().put("widgeLayout", wqidgeLayout);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("widge:widge-layout:save")
    public R save(@RequestBody WidgeLayoutEntity widgeLayout){
        widgeLayout.setCreated(new Date());
		widgeLayoutService.save(widgeLayout);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("widge:widge-layout:update")
    public R update(@RequestBody WidgeLayoutEntity widgeLayout){
        widgeLayout.setUpdated(new Date());
		widgeLayoutService.updateById(widgeLayout);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("widge:widge-layout:delete")
    public R delete(@RequestBody Long[] ids){
		widgeLayoutService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
